package com.huiyujinrong.qiangdanbao.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.huiyujinrong.qiangdanbao.R;

import java.util.List;

/**
 * 发现瀑布流
 * Created by ZQXQ-Developer on 2017/6/28.
 */

public class FindAdapter extends ListBaseAdapter {
    private List<String> items;
    private Context mContext;
    public OnItemClickListener mItemClickListener;
    private LayoutInflater inflater;


    public FindAdapter setmItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
        return this;
    }

    public FindAdapter(Context context, List<String> items) {
        this.mContext = context;
        this.items = items;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(viewType, parent, false);
        return new EImageViewHolder(view, mItemClickListener);
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.item_find_card;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((EImageViewHolder) holder).bind(items.get(position));
    }

    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }


    public interface OnItemClickListener {
        void onItemClickListener(View view);
    }

    /**
     * 展业神器
     */
    private class EImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView eImageView;
        private OnItemClickListener clickListener;

        public EImageViewHolder(View view, OnItemClickListener mItemClickListener) {
            super(view);
            eImageView = (ImageView) view.findViewById(R.id.imFind);
            this.clickListener = mItemClickListener;
            eImageView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                clickListener.onItemClickListener(view);
            }
        }

        public void bind(String eImage) {
            int width = ((Activity) eImageView.getContext()).getWindowManager().getDefaultDisplay().getWidth();
            ViewGroup.LayoutParams params = eImageView.getLayoutParams();
            params.width = width / 2;
            params.height = (int) (200 + Math.random() * 600);
            eImageView.setLayoutParams(params);
            Glide.with(mContext).load(eImage).into(eImageView);
        }
    }

}